import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Match extends Applet implements ActionListener{

	public Match()
	{
		
	}
	
	
	
	private JFrame frame;
	private JLabel label, p1, p2, p3, p4;
	private JButton button2, button3, button4;
	private JButton p1inc, p1dec, p2inc, p2dec, p3inc, p3dec, p4inc, p4dec, acceptName;
	private JButton p1minc, p1mdec, p2minc, p2mdec, p3minc, p3mdec, p4minc, p4mdec;
	private JPanel panel, p1panel, p2panel, p3panel, p4panel, northPanel, southPanel;
	
	
	
	private int numOfPlayers;
	private String player1stat, player2stat, player3stat, player4stat;
	private JTextField p1text, p2text, p3text, p4text;

	Player player1, player2, player3, player4;
	/**
	 * @param args
	 */
	public void init() {
		// TODO Auto-generated method stub

		
		
		frame = new JFrame("Choosing Number of Players");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//JWindow win = new JWindow(frame);
		label = new JLabel("Number of Players");
		panel = new JPanel();
		
		panel.add(label);
		
		button2 = new JButton("2");
		button3 = new JButton("3");
		button4 = new JButton("4");
		
		//win.setLayout(new FlowLayout());
		frame.setSize(500, 500);
		panel.add(label);
		
		panel.add(button2);
		panel.add(button3);
		panel.add(button4);
		frame.setBackground(Color.BLACK);
		//this.setVisible(true);
		//this.getAppletContext();
		//this.start();
		
		
		
		
		button2.addActionListener(this);
		button3.addActionListener(this);
		button4.addActionListener(this);
		

		
		button2.setVisible(true);
		button3.setVisible(true);
		button4.setVisible(true);
		
		//label.setVisible(true);
		
		frame.add(panel, BorderLayout.CENTER);
		frame.setVisible(true);
		
		
	}
	
	public void paint(Graphics g)
	{
		g.create();
		g.setColor(Color.BLACK);
	}

	@Override
	public void actionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
		
		if(evt.getSource() == button2)
		{
			numOfPlayers = 2;
			nameScreen();
		}
		else if(evt.getSource() == button3)
		{
			numOfPlayers = 3;
			nameScreen();
		}
		else if(evt.getSource() == button4)
		{
			numOfPlayers = 4;
			nameScreen();
		}
		else if(evt.getSource() == acceptName)
		{
			gameScreen();
		}
		
		
	}
	
	public void start()
	{
	}

	public void run()
	{
		
	}
	
	private void nameScreen()
	{
		frame.removeAll();
		frame.setVisible(false);
		panel.removeAll();
		
		frame = new JFrame("Player's Name's");
		frame.setSize(500, 500);
		frame.setBackground(Color.black);
		panel = new JPanel();
		
		/*String test = "Number of players is: " + numOfPlayers;
		label = new JLabel(test);
		panel.add(label);
		frame.add(panel, BorderLayout.CENTER);*/
		
		p1text = new JTextField(15);
		p2text = new JTextField(15);
		p3text = new JTextField(15);
		p4text = new JTextField(15);
		
		p1 = new JLabel("Player 1's name:");
		p2 = new JLabel("Player 2's name:");
		p3 = new JLabel("Player 3's name:");
		p4 = new JLabel("Player 4's name:");
		
		acceptName = new JButton("OK");
		
		p1panel = new JPanel();
		p2panel = new JPanel();
		p3panel = new JPanel();
		p4panel = new JPanel();
		
		northPanel = new JPanel();
		southPanel = new JPanel();
		northPanel.setLayout( new BorderLayout());
		southPanel.setLayout( new BorderLayout());
		
		
		switch(numOfPlayers)
		{
		case 2:
			p1panel.add(p1);
			p1panel.add(p1text);
			p2panel.add(p2);
			p2panel.add(p2text);
			frame.add(p1panel, BorderLayout.NORTH);
			frame.add(p2panel, BorderLayout.CENTER);
			break;
		case 3:
			p1panel.add(p1);
			p1panel.add(p1text);
			p2panel.add(p2);
			p2panel.add(p2text);
			p3panel.add(p3);
			p3panel.add(p3text);
			northPanel.add(p1panel, BorderLayout.NORTH);
			northPanel.add(p2panel, BorderLayout.SOUTH);
			frame.add(northPanel, BorderLayout.NORTH);
			frame.add(p3panel, BorderLayout.CENTER);
			break;
		case 4:
			p1panel.add(p1);
			p1panel.add(p1text);
			p2panel.add(p2);
			p2panel.add(p2text);
			p3panel.add(p3);
			p3panel.add(p3text);
			p4panel.add(p4);
			p4panel.add(p4text);
			northPanel.add(p1panel, BorderLayout.NORTH);
			northPanel.add(p2panel, BorderLayout.SOUTH);
			southPanel.add(p3panel, BorderLayout.NORTH);
			southPanel.add(p4panel, BorderLayout.CENTER);
			frame.add(northPanel, BorderLayout.NORTH);
			frame.add(southPanel, BorderLayout.CENTER);
			break;
		}
		
		frame.add(acceptName, BorderLayout.SOUTH);
		acceptName.addActionListener(this);
		
		
		
		
		
		
		
		frame.setVisible(true);
		
		
		
		
		
	}
	
	private void gameScreen()
	{
		switch(numOfPlayers)
		{
		case 4:
			player1 = new Player(p1text.getText());
			player2 = new Player(p2text.getText());
			player3 = new Player(p3text.getText());
			player4 = new Player(p4text.getText());
			break;
		}
		frame.removeAll();
		panel.removeAll();
		frame = new JFrame("Game Screen");
		frame.setSize(1000, 500);
		
		panel = new JPanel();
		panel.setLayout(new GridBagLayout());
		
		
		
		
		JLabel team1 = new JLabel("Team 1");
		team1.setFont(new Font("SansSerif", Font.BOLD, 40));
		JLabel team2 = new JLabel("Team 2");
		team2.setFont(new Font("SansSerif", Font.BOLD, 40));
		
		//change all add methods
		//panel.add(team1, gc);
		//panel.add(team2, gc);
		
		int centered = GridBagConstraints.CENTER;
		
		addItem(panel, team1, 0, 0, 1, 1, centered);
		addItem(panel, team2, 1, 0, 1, 1, centered);
		
		
		double p1num = (player1.getShotsMade()*100.0/player1.getShotAttempts());
		int p1int = (int)(p1num * 100);
		p1num = p1int/100.0;
		
		double p2num = (player2.getShotsMade()*100.0/player2.getShotAttempts());
		int p2int = (int)(p2num * 100);
		p2num = p2int/100.0;
		
		double p3num = (player3.getShotsMade()*100.0/player3.getShotAttempts());
		int p3int = (int)(p3num * 100);
		p3num = p3int/100.0;
		
		double p4num = (player4.getShotsMade()*100.0/player4.getShotAttempts());
		int p4int = (int)(p4num * 100);
		p4num = p4int/100.0;
		
		
		player1stat = new String(player1.getShotsMade() + "/" + player1.getShotAttempts() + " = " + p1num + "%");
		player2stat = new String(player2.getShotsMade() + "/" + player2.getShotAttempts() + " = " + p2num + "%");
		player3stat = new String(player3.getShotsMade() + "/" + player3.getShotAttempts() + " = " + p3num + "%");
		player4stat = new String(player4.getShotsMade() + "/" + player4.getShotAttempts() + " = " + p4num + "%");
		
		
		
		addItem(panel, new JLabel(player1.getName()), 0, 1, 1, 1, centered);
		addItem(panel, new JLabel(player2.getName()), 1, 1, 1, 1, centered);
		addItem(panel, new JLabel(player3.getName()), 2, 1, 1, 1, centered);
		addItem(panel, new JLabel(player4.getName()), 3, 1, 1, 1, centered);

		addItem(panel, new JLabel(player1stat), 0, 2, 1, 1, centered);
		addItem(panel, new JLabel(player2stat), 1, 2, 1, 1, centered);
		addItem(panel, new JLabel(player3stat), 2, 2, 1, 1, centered);
		addItem(panel, new JLabel(player4stat), 3, 2, 1, 1, centered);
		
		//frame.add(panel, BorderLayout.NORTH);
		//frame.add(panel2, BorderLayout.CENTER);
		
		//JPanel panel3 = new JPanel();
		//panel3.setLayout(new GridLayout(0, 4));
		
		p1inc = new JButton("+");
		p2inc = new JButton("+");
		p3inc = new JButton("+");
		p4inc = new JButton("+");
		
		p1dec = new JButton("-");
		p2dec = new JButton("-");
		p3dec = new JButton("-");
		p4dec = new JButton("-");
		
		
		p1minc = new JButton("+");
		p2minc = new JButton("+");
		p3minc = new JButton("+");
		p4minc = new JButton("+");
		
		p1mdec = new JButton("-");
		p2mdec = new JButton("-");
		p3mdec = new JButton("-");
		p4mdec = new JButton("-");
		
		addItem(panel, new JLabel("Shot made"), 0, 3, 1, 1, centered);
		addItem(panel, new JLabel("Shot made"), 1, 3, 1, 1, centered);
		addItem(panel, new JLabel("Shot made"), 2, 3, 1, 1, centered);
		addItem(panel, new JLabel("Shot made"), 3, 3, 1, 1, centered);
		
		
		addItem(panel, p1dec, 0, 4, 1, 1, centered);
		addItem(panel, p1inc, 1, 4, 1, 1, centered);
		addItem(panel, p2dec, 2, 4, 1, 1, centered);
		addItem(panel, p2inc, 3, 4, 1, 1, centered);
		addItem(panel, p3dec, 4, 4, 1, 1, centered);
		addItem(panel, p3inc, 5, 4, 1, 1, centered);
		addItem(panel, p4dec, 6, 4, 1, 1, centered);
		addItem(panel, p4inc, 7, 4, 1, 1, centered);
		
		addItem(panel, new JLabel("Shot missed"), 0, 5, 1, 1, centered);
		addItem(panel, new JLabel("Shot missed"), 1, 5, 1, 1, centered);
		addItem(panel, new JLabel("Shot missed"), 2, 5, 1, 1, centered);
		addItem(panel, new JLabel("Shot missed"), 3, 5, 1, 1, centered);
		
		addItem(panel, p1mdec, 0, 6, 1, 1, centered);
		addItem(panel, p1minc, 1, 6, 1, 1, centered);
		addItem(panel, p2mdec, 2, 6, 1, 1, centered);
		addItem(panel, p2minc, 3, 6, 1, 1, centered);
		addItem(panel, p3mdec, 4, 6, 1, 1, centered);
		addItem(panel, p3minc, 5, 6, 1, 1, centered);
		addItem(panel, p4mdec, 6, 6, 1, 1, centered);
		addItem(panel, p4minc, 7, 6, 1, 1, centered);
		
		p1inc.addActionListener(this);
		p1dec.addActionListener(this);
		p1minc.addActionListener(this);
		p1mdec.addActionListener(this);
		
		p2inc.addActionListener(this);
		p2dec.addActionListener(this);
		p2minc.addActionListener(this);
		p2mdec.addActionListener(this);
		
		p3inc.addActionListener(this);
		p3dec.addActionListener(this);
		p3minc.addActionListener(this);
		p3mdec.addActionListener(this);
		
		p4inc.addActionListener(this);
		p4dec.addActionListener(this);
		p4minc.addActionListener(this);
		p4mdec.addActionListener(this);
		
		frame.add(panel);
		
		frame.setVisible(true);
	}
	
	private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align)
	{
		GridBagConstraints gc = new GridBagConstraints();
		gc.anchor = align;
		gc.gridx = x;
		gc.gridy = y;
		gc.gridwidth = width;
		gc.gridheight = height;
		gc.insets = new Insets(5, 5, 5, 5);
		gc.fill = GridBagConstraints.NONE;
		p.add(c, gc);
	}
	/*
	 * switch(numOfPlayers)
		{
		case 4:
			player4inc = new JButton("+");
			player4dec = new JButton("-");
		case 3:
			player3inc = new JButton("+");
			player3dec = new JButton("-");
		case 2:
			player2inc = new JButton("+");
			player2dec = new JButton("-");
			player1inc = new JButton("+");
			player1dec = new JButton("-");
			break;
		}
	 */

}
